package com.hclc.util.date;

import com.hclc.util.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.CollectionUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName PerpetualCalendarUtil
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 15:30 2020/6/10
 * @version: 1.0
 */
@Slf4j
public class PerpetualCalendarUtil {
    /**
     * 当年国定节假日
     */
    private static Set<String> holidaySet = new HashSet();

    /**
     * 百度日期api接口
     */
    private static String PERPETUAL_CALENDAR_URL = "http://opendata.baidu.com/api.php?query=FORMAT_DATE&resource_id=6018&format=json";

    /**
     * 放假
     */
    private static final String HOLIDAY = "1";

    /**
     * 补班
     */
    private static final String MAKE_UP = "2";

    /**
     * 日期格式化规则
     */
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d");

    /**
     * 正确响应状态
     */
    private static String API_SUCCESS_STATUS = "0";

    /**
     * 获取当年节假日set集合
     *
     * @param
     * @return java.util.Set<java.lang.String>
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/6/10 17:27
     */

    public static Set<String> getCurrentYearHolidays(LocalDate localDate) {
        if (!holidaySet.isEmpty()) {
            LocalDate setLocalDate = LocalDate.parse(holidaySet.iterator().next(), formatter);
            if (localDate.getYear() == setLocalDate.getYear()) {
                return holidaySet;
            }
        }
        holidaySet.clear();
        //当年所有法定节假日(注:周六周日属于公休日，不是法定节假日)
        PERPETUAL_CALENDAR_URL = PERPETUAL_CALENDAR_URL.replace("FORMAT_DATE", localDate.toString());
        JSONObject resultData = JSONObject.fromObject(HttpUtils.doGet(PERPETUAL_CALENDAR_URL));
        try {
            JSONArray dataArr = API_SUCCESS_STATUS.equals(resultData.get("status")) ? ((JSONArray) JSONObject.fromObject(resultData).get("data")) : null;
            if (!CollectionUtils.isEmpty(dataArr)) {
                JSONObject holidayObject = (JSONObject) dataArr.get(0);
                JSONArray holidayArr = (JSONArray) holidayObject.get("holiday");
                holidayArr.stream().forEach(arr -> {
                    ((JSONArray) ((JSONObject) arr).get("list")).stream().forEach(time -> {
                        JSONObject timeJson = (JSONObject) time;
                        if (HOLIDAY.equals(timeJson.get("status"))) {
                            holidaySet.add(LocalDate.parse(timeJson.getString("date"), formatter).toString());
                        }
                    });
                });
            }
        } catch (Exception e) {
            log.error("获取当年节假日set集合解析异常,url:{}", PERPETUAL_CALENDAR_URL, e);
        }
        return holidaySet;
    }

    /**
     * 判断指定日期是否是周末
     *
     * @param localDate
     * @return boolean
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/6/10 17:54
     */

    public static boolean currentDayIsWeekend(LocalDate localDate) {
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
    }

    /**
     * 判断指定日期是否是工作日
     *
     * @param
     * @return boolean
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/6/10 17:48
     */

    public static boolean currentDayIsWorkingDay(LocalDate localDate) {
        return !(currentDayIsWeekend(localDate) || getCurrentYearHolidays(localDate).contains(localDate.toString()));
    }


    public static void main(String[] args) {
        LocalDate now = LocalDate.of(2020,8,8);
        System.out.println(currentDayIsWeekend(now));
        System.out.println(currentDayIsWorkingDay(now));
        System.out.println(getCurrentYearHolidays(now));
    }
}
